<?php include_stylesheets_for_form($form) ?>
<?php include_javascripts_for_form($form) ?>
<script type="text/javascript">
// <![CDATA[
function getLabel(fieldName){
	var labels = {
	  <?php
	  $count = 0;
	  foreach($form as $formfield){
			$key = $formfield->renderName();
			$val = $formfield->renderLabelName();
			echo "'". html_entity_decode(htmlentities($key, ENT_QUOTES)) ."':'". html_entity_decode(htmlentities($val, ENT_QUOTES)) ."'". (($form->count() - 1 == $count++) ? '' : ','); 
	  }
	  ?>
	};
	return labels[fieldName];
}
</script>
<style type="text/css">
<!--
Correction de css pour gerer la place des fournisseurs.
-->

#wrapperfournisseurs{
	float: left;
    width: 50%;
}

#wrapperfournisseurs{
	float: left;
    width: 50%;
}
.clear{
	clear: both;
}
</style>

<?php if(!isset($hide))
	$hide = array(); 
	$hide[] = 'datecreation';
	if($sf_request->isXmlHttpRequest()){
		$formAction = url_for($module.'/'.($form->getObject()->isNew() ? 'addItem' : 'updateItem').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '').'&className='.$className.'&alias='.$alias);
	}
	?>


<?php if(!isset($buttons)) $buttons=true?>
<?php if(!isset($formAction)){
	$formAction = url_for($module.'/'.($form->getObject()->isNew() ? 'create?className='.$className : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId().'&className='.$className : ''));
}?>
<?php if(!isset($target)){
	$target='';
}else{
	$target = "target=$target";
}?>

<form id="clientForm" action="<?php echo $formAction ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?> tmt:validate = "true" tmt:callback="errorOnLabels" class="defaultForm <?php echo $target;?>">
<?php if (!$form->getObject()->isNew()): ?>
<input type="hidden" name="sf_method" value="put" />
<?php endif; ?>

<div id="errorDisplayHaut" class="errorDisplay">&nbsp;</div>
<?php echo $form->renderGlobalErrors() ?>
<?php if($default=='true'):?>
			<?php  if ($form->isCSRFProtected()) : ?> 
			  <?php echo $form['_csrf_token']; ?> 
			<?php endif; ?>							
			<?php echo $form['id']; ?>
		<?php //if(empty($objDef)) $objDef = new objectDef($className); ?>			
		<?php if($objDef->description): ?>
			<fieldset>
				<legend><?php echo __("General Information"); ?></legend>
		  			<div class="description">
		  				<?php echo $objDef->description?>
		  			</div>
		  	</fieldset>
		<?php endif;?>
		  <?php 
	   	  $section = '';
		  foreach($objDef->attributes as $fieldName => $att):?>
		    <?php if($section != $att['section']):?>
	        	<?php if($section !=''):?>
	        		</fieldset>
	        	<?php endif;?>
	            <fieldset id="<?php echo Util::stripAccents2($att['section'], true)?>"><legend><?php echo __($att['section'],null,'forms')?></legend>
	  		<?php endif;?>
		    <?php if($att['type']=='BOList'):?>
				  <?php $relatedClass = $att['relatedClass']; $relatedObjDef = new objectDef($relatedClass);?>
				  <fieldset><legend><?php echo $att['label']?></legend>
				  <?php if($form->getObject()->isNew()){$paramId='';}else{$paramId='&ownerId='.$form->getObject()->id;}?>
				  <div id="list_<?php echo $fieldName?>"></div>
				  		<?php $boLists[$fieldName] = $relatedClass?>
				  </fieldset>
			<?php else:?>
			  	<?php echo $objDef->renderAttributeWidget($form, $fieldName, $hide); ?> 
	  		<?php endif;?>
	  		<?php if($fieldName=='remises'):?><div class="clear"></div><?php endif;?>
	  		<?php $section = $att['section']?>
		  <?php endforeach;?>
		  </fieldset>
		  <?php if($buttons):?>
		  	<?php include_partial('defaultViewer/buttons', array('form' => $form,'module' => $module,'className' => $className, 'default'=>$default))  ?>
		  <?php endif;?>
<?php endif;?>


<?php 
if(isset($form->extraTemplates)){
	if(is_array($form->extraTemplates)){
		foreach($form->extraTemplates as $template){
			include_partial($template, array('form'=>$form)); 
		}
	}
}
?>
</form>
<?php if(isset($boLists)):?>
<?php //For each items list, we must create a div to contain the form, and we must retrive the list of existing items.?>
<?php foreach($boLists as $fieldName => $class): ?>
	<div id="<?php echo $fieldName.'Form'?>" class="editItemForm" style="display:none"></div>
	
	<script type="text/javascript">
		xhr<?php print $fieldName?> = getXmlHttpRequest2();
		xhr<?php print $fieldName?>.onreadystatechange = function()
		{
			if(xhr<?php print $fieldName?>.readyState == 4 && xhr<?php print $fieldName?>.status == 200)
			{
				var htmlContent = xhr<?php print $fieldName?>.responseText;
				document.getElementById("list_<?php print $fieldName?>").innerHTML = htmlContent;
				$("a.fancy").fancybox({
					'transitionIn'	:	'elastic',
					'transitionOut'	:	'elastic',
					'speedIn'		:	600, 
					'speedOut'		:	200, 
					'autoDimensions'	: true,
					'overlayShow'	:	false,
					'titleShow'		: 	false,
					'overlayShow'	: 	true,
					ajax: { 
                        cache    : false
                    }
				});

				//modal : true
			}
		};

		xhr<?php print $fieldName?>.open("POST","<?php print url_for($module.'/getViewItems'); ?>",true);
		xhr<?php print $fieldName?>.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr<?php print $fieldName?>.send("ownerClass=<?php print get_class($form->getObject())?>&ownerId=<?php print $form->getObject()->getId()?>&fieldName=<?php print $fieldName?>&className=<?php print $class?>");


	</script>
<?php endforeach;?>
<?php endif;?>
	<?php if($objDef->hasAttach):?>
		<?php include_partial('attachments/form', array( 'form' => $formAttach)) ?>
	<?php endif;?>
	
<script type="text/javascript">

function submitForm(idForm){
	var result = tmt.validator.validateForm(idForm);
	if(result){
		document.forms[idForm].submit();
	}
}

function renderList(className, fieldName, varResult)
{	
	
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('clients/getViewItems'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhr.send("ownerClass=<?php print get_class($form->getObject())?>&ownerId=<?php print $form->getObject()->getId()?>&fieldName="+ fieldName +"&className="+className);
	var htmlTable = xhr.responseText;
	document.getElementById('list_'+ fieldName).innerHTML = htmlTable;
	$("a.fancy").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titleShow'		: false
	});

}

function deleteItem(idDiv, className, fieldName, id)
{
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('defaultViewer/deleteItem'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhr.send("fieldName="+fieldName+'&id='+id);
	var html = xhr.responseText;
	if(html==""){
		varResult = 0;
	}else{
		varResult = 1;
	}
	renderList(className, fieldName, varResult);
	
}	
</script>

